<template>
  <div class="calendar">
    <div style="text-align: right">
      <el-select v-model="year" @change="changeDate">
        <el-option v-for="i in yearArr" :key="i" :label="i" :value="i" />
      </el-select>
      <el-select v-model="month" @change="changeDate">
        <el-option v-for="i in 12" :key="i" :label="i" :value="i" />
      </el-select>
    </div>
    <el-calendar :value="value">
      <template #dateCell="{ data }">
        <div style="width: 100%; height: 100%" @click="changeDay(data.day)">
          <span>{{ data.day.split('-')[2] }}</span>
          <span v-if="showDay(data)" class="rleax">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: Date,
      default: () => {
        return new Date()
      }
    }
  },
  data() {
    return {
      year: 2021,
      yearArr: [],
      month: 12,
      localData: new Date()
    }
  },
  watch: {
    value: {
      handler(newVal) {
        this.year = newVal.getFullYear()
        this.month = newVal.getMonth() + 1
        const min = this.year - 5
        const max = this.year + 5
        this.yearArr = []
        for (let i = min; i < max; i++) {
          this.yearArr.push(i)
        }
      },
      immediate: true
    }
  },
  methods: {
    showDay(data) {
      const t = new Date(data.day)
      const day = t.getDay()
      const bol = data.type === 'current-month'
      if (day === 0 || day === 6) {
        return bol && true
      } else {
        return false
      }
    },
    changeDate() {
      this.$emit('input', new Date(`${this.year}-${this.month}-01`))
    },
    changeDay(day) {
      this.$emit('input', new Date(day))
    }
  }
}
</script>

<style lang="scss" scoped>
.calendar {
  ::v-deep .el-calendar__header {
    display: none;
  }
  .rleax {
    background: #fa7c4d;
    display: inline-block;
    position: relative;
    top: -1px;
    width: 20px;
    height: 20px;
    line-height: 20px;
    font-size: 10px;
    border-radius: 50%;
    color: #fff;
    text-align: center;
    margin-left: 10px;
  }
}
</style>
